package com.liankuan.console.service.ServiceImpl;

import com.liankuan.console.controller.vo.AddExchangeVO;
import com.liankuan.console.dto.Exchange;
import com.liankuan.console.dto.UserExchange;
import com.liankuan.console.repository.ExchangeRepository;
import com.liankuan.console.repository.StrategyExchangeRepository;
import com.liankuan.console.service.ExchangeService;
import com.liankuan.console.service.UserExchangeService;
import com.liankuan.console.util.DES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestAttribute;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExchangeServiceImpl implements ExchangeService {

    private final ExchangeRepository exchangeRepository;
    private UserExchangeService userExchangeService;
    private StrategyExchangeRepository strategyExchangeRepository;

    @Autowired
    public ExchangeServiceImpl(ExchangeRepository exchangeRepository, UserExchangeService userExchangeService,
                               StrategyExchangeRepository strategyExchangeRepository) {
        this.exchangeRepository = exchangeRepository;
        this.userExchangeService = userExchangeService;
        this.strategyExchangeRepository=strategyExchangeRepository;
    }

    /**
     * 获取所有的交易所信息
     *
     * @return
     */
    @Override
    public List<Exchange> exchangeLists() {
        return exchangeRepository.findAll();
    }

    /**
     * 根据id获取交易所信息
     *
     * @param exchangeId
     * @return
     */
    @Override
    public Exchange findById(int exchangeId) {
        return exchangeRepository.findById(exchangeId);
    }

    /**
     * 更新交易所信息
     *
     * @param exchange
     */
    @Override
    public void updateExchangeInfo(Exchange exchange) {
        //信息加密处理
//        Exchange exchange1=getMd5Exchange(exchange);
        exchangeRepository.save(exchange);
    }

    /**
     * 对交易所公钥和秘钥信息进行加密处理
     *
     * @param exchange
     * @return
     */
    private Exchange getMd5Exchange(Exchange exchange) {
        //获取一个往数据库中存储的key
        String key = DES.getKey();
        String md5Key = DES.getMD5String(key);
        String publicKey = DES.encrypt(exchange.getPublicKey(), md5Key);
        String privateKey = DES.encrypt(exchange.getPrivateKey(), md5Key);

        //加密完之后重新为交易所信息赋值
        exchange.setPublicKey(publicKey);
        exchange.setPrivateKey(privateKey);
        exchange.setDecodeKey(key);

        return exchange;
    }

    /**
     * 删除某条交易所信息(------>直接删除所有该交易所的关系)
     *
     * @param exchangeId
     */
    @Override
    @Transactional
    public void deleteExchangeInfo(int userId, int exchangeId) {
        //1.首先删除用户和交易所的关联信息(直接删除所有该交易所的关系)
        userExchangeService.deleteByExchangeId(exchangeId);

        //2.删除策略和该交易所的关联-->(直接删除所有该交易所的关系)
        strategyExchangeRepository.deleteAllByExchangeId(exchangeId);

        //3.删除交易所表中数据
        exchangeRepository.delete(exchangeId);
    }

    /**
     * 新增交易所信息
     *
     * @param exchange
     */
    @Override
    @Transactional
    public void addExchange(Exchange exchange, int userId) {
        //1.首先信息加密处理
        Exchange exchange1 = getMd5Exchange(exchange);

        exchangeRepository.save(exchange1);

        //2.进行关联表的新增
        UserExchange userExchange = new UserExchange();
        userExchange.setUserId(userId);
        userExchange.setExchangeId(exchange1.getId());

        userExchangeService.save(userExchange);
    }

    /**
     * 获取所有的交易所信息
     *
     * @return
     */
    @Override
    public List<AddExchangeVO> findAllExchange() {
        return exchangeRepository.findAll().stream().map(AddExchangeVO::new).collect(Collectors.toList());
    }

    /**
     * 根据交易所id集合去获取所有的交易所
     *
     * @param exchangeIds
     * @return
     */
    @Override
    public List<Exchange> findAllExchangeByIds(List<Integer> exchangeIds) {
        return exchangeRepository.findAllByIdInOrderById(exchangeIds);
    }

    /**
     * 根据用户的id获取对应的交易所信息VO
     *
     * @param userId
     * @return
     */
    @Override
    public List<AddExchangeVO> findAllExchangeByUserId(Integer userId) {
        List<Integer> exchangeIds = userExchangeService.findAllExchanges(userId);
        List<Exchange> exchangeList = findAllExchangeByIds(exchangeIds);

        return exchangeList.stream().map(AddExchangeVO::new).collect(Collectors.toList());
    }
}
